﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.Controls.Joints.CUiDeviceNodeData
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using Tecnomatix.Engineering;
using UiInternalControls.WPF;


namespace DnProcessSimulateCommands.RobotJog.Controls.Joints;

public class CUiDeviceNodeData : CUiNodeData
{
  private List<CUiNodeData> m_childrenNodes = new List<CUiNodeData>();
  private TxObjectList _registeredPoses = new TxObjectList();
  private Dictionary<string, ITxDevice> m_otherDevice = new Dictionary<string, ITxDevice>();
  private bool m_registeredForSelectedIndexChanged;

  public event CUiDeviceNodeData.CUiRJRobotJogDeviceNodeDataJointChangedHandler JointChanged;

  public CUiDeviceNodeData(CApRJRobotJogJointsLogic jointJogApplication, ITxDevice device)
    : base(jointJogApplication, (ITxObject) device, (ImageSource) null, ((ITxObject) device).Name)
  {
    this.m_nodeIcon = TxIconManager.the().GetObjectImageSource((ITxObject) device);
    this.AddJoints();
    this.AddChildren();
    this.RegisterForEvents();
  }

  public ITxDevice Device => this.NodeObject as ITxDevice;

  public List<CUiNodeData> ChildrenNodes => this.m_childrenNodes;

  public IEnumerable<CUiJointNodeData> Joints
  {
    get
    {
      foreach (CUiNodeData childrenNode in this.m_childrenNodes)
      {
        if (childrenNode is CUiJointNodeData joint && !joint.Disposed)
          yield return joint;
      }
    }
  }

  public IEnumerable<CUiDeviceNodeData> ChildDevices
  {
    get
    {
      foreach (CUiNodeData childrenNode in this.m_childrenNodes)
      {
        if (childrenNode is CUiDeviceNodeData childDevice && !childDevice.Disposed)
          yield return childDevice;
      }
    }
  }

  private void nodeObject_KinematicMoved(object sender, TxDevice_KinematicMovedEventArgs args)
  {
    foreach (CUiJointNodeData joint in this.Joints)
    {
      joint.EnterControlsInitMode();
      bool flag = false;
      if (joint.HasVariableLimits())
      {
        joint.StartRefreshControls();
        flag = true;
      }
      if (!joint.Joint.IsDependent)
      {
        ITxDevice device = joint.Joint.Device;
        TxDevicePoseData txDevicePoseData = new TxDevicePoseData(device, device.CurrentPose);
        if (txDevicePoseData.DoesHaveJoint(joint.Joint))
        {
          double jointValue = txDevicePoseData.GetJointValue(joint.Joint);
          joint.JointValue = jointValue;
        }
      }
      if (flag)
        joint.EndRefreshControls();
      joint.ExitControlsInitMode();
    }
  }

  private void ValueControl_ValueChanged(object sender, EventArgs e)
  {
    if (this.JointChanged == null)
      return;
    this.JointChanged(sender, e);
  }

  private void SliderControl_ValueChanged(object sender, TxWheel_ValueChangedEventArgs e)
  {
    if (this.JointChanged == null)
      return;
    EventArgs args = new EventArgs();
    this.JointChanged(sender, args);
  }

  private void SliderControl_DragWheelStart(object sender, EventArgs e)
  {
    TxApplication.ActiveUndoManager.StartTransaction();
  }

  private void SliderControl_DragWheelEnd(object sender, EventArgs e)
  {
    TxApplication.ActiveUndoManager.EndTransaction();
  }

  private void deviceData_JointChanged(object sender, EventArgs args)
  {
    if (this.JointChanged == null)
      return;
    this.JointChanged(sender, args);
  }

  private TxPose GetPoseByPoseData(TxPoseData poseData, TxObjectList posesList)
  {
    TxPose poseByPoseData = (TxPose) null;
    TxDevicePoseData txDevicePoseData1 = new TxDevicePoseData(this.Device, poseData);
    foreach (TxPose poses in (Collection<ITxObject>) posesList)
    {
      TxDevicePoseData txDevicePoseData2 = new TxDevicePoseData(this.Device, poses.PoseData);
      if (txDevicePoseData1.Equals((object) txDevicePoseData2))
      {
        poseByPoseData = poses;
        break;
      }
    }
    return poseByPoseData;
  }

  public void SetJointValue(TxJoint joint, double newValue)
  {
    if (CApRJRobotJogJointsLogic.JointSpecific_AreEqualValues(joint.CurrentValue, newValue))
      return;
    this.m_jointJogApplication.FireDeviceManipulatedFromInternalCommand(true);
    this.Device.KinematicMoved -= new TxDevice_KinematicMovedEventHandler(this.nodeObject_KinematicMoved);
    this.UnregisterForOtherDevicesEvents();
    double currentValue = joint.CurrentValue;
    if (CApRJSRobotsManager.the().GetRobotMasterSlaveMode() && CApRJSRobotsManager.the().IsMasterExist())
      CApRJSRobotsManager.the().RecordCurrentRobotData();
    this.m_jointJogApplication.FireSaveOldLocations();
    this.m_jointJogApplication.FireBeforeJointValueChange(joint);
    joint.CurrentValue = newValue;
    if (!this.m_jointJogApplication.FireJointValueChanged(joint))
      joint.CurrentValue = currentValue;
    this.m_jointJogApplication.FireAfterJointValueChange(joint);
    this.Device.KinematicMoved += new TxDevice_KinematicMovedEventHandler(this.nodeObject_KinematicMoved);
    this.RegisterForOtherDevicesEvents();
    this.m_jointJogApplication.FireDeviceManipulatedFromInternalCommand(false);
  }

  public void ChangeDevicePose(TxPoseData poseData) => this.Device.CurrentPose = poseData;

  private void AddJoints()
  {
    foreach (TxJoint drivingJoint in (Collection<ITxObject>) this.Device.DrivingJoints)
      this.m_childrenNodes.Add((CUiNodeData) this.m_jointJogApplication.GetJointNodeData(drivingJoint, this, this.Device));
    foreach (TxJoint joint in (Collection<ITxObject>) this.Device.AllJointsAfterCompilation)
    {
      if (joint.IsDependent)
        this.m_childrenNodes.Add((CUiNodeData) this.m_jointJogApplication.GetJointNodeData(joint, this, this.Device));
    }
    TxRobot nodeObject = this.NodeObject as TxRobot;
    if (!TxRobot.op_Inequality(nodeObject, (ITxObject) null))
      return;
    foreach (TxJoint externalAx in (Collection<ITxObject>) nodeObject.ExternalAxes)
    {
      ITxDevice deviceOfJoint = this.GetDeviceOfJoint(externalAx);
      CUiJointNodeData jointNodeData = this.m_jointJogApplication.GetJointNodeData(externalAx, this, this.Device, true);
      if (deviceOfJoint == nodeObject)
      {
        jointNodeData.IsExternalJoint = true;
      }
      else
      {
        jointNodeData.NiceName = $"{jointNodeData.Joint.Name} ({((ITxObject) jointNodeData.Joint.Device).Name})";
        this.m_childrenNodes.Add((CUiNodeData) jointNodeData);
        if (!this.m_otherDevice.ContainsKey(((ITxObject) deviceOfJoint).Id))
          this.m_otherDevice.Add(((ITxObject) deviceOfJoint).Id, deviceOfJoint);
      }
    }
  }

  private ITxDevice GetDeviceOfJoint(TxJoint joint)
  {
    ITxDevice deviceOfJoint = (ITxDevice) null;
    TxKinematicLink childLink = joint.ChildLink;
    if (TxKinematicLink.op_Inequality(childLink, (ITxObject) null))
    {
      ITxObjectCollection collection = childLink.Collection;
      if (collection != null)
        deviceOfJoint = collection as ITxDevice;
    }
    return deviceOfJoint;
  }

  private void AddChildren()
  {
    if (!(this.NodeObject is ITxObjectCollection nodeObject))
      return;
    TxTypeFilter txTypeFilter1 = new TxTypeFilter(typeof (ITxComponent));
    foreach (ITxComponent directDescendant in (Collection<ITxObject>) nodeObject.GetDirectDescendants((ITxTypeFilter) txTypeFilter1))
    {
      if (directDescendant is ITxDevice device)
        this.m_childrenNodes.Add((CUiNodeData) this.m_jointJogApplication.GetDeviceNodeData(device));
      else if (directDescendant is ITxObjectCollection objectCollection)
      {
        TxTypeFilter txTypeFilter2 = new TxTypeFilter(typeof (ITxDevice));
        if (((Collection<ITxObject>) objectCollection.GetAllDescendants((ITxTypeFilter) txTypeFilter2)).Count > 0)
          this.m_childrenNodes.Add((CUiNodeData) this.m_jointJogApplication.GetComponentNodeData(directDescendant));
      }
    }
  }

  private void RegisterForOtherDevicesEvents()
  {
    foreach (KeyValuePair<string, ITxDevice> keyValuePair in this.m_otherDevice)
      keyValuePair.Value.KinematicMoved += new TxDevice_KinematicMovedEventHandler(this.device_KinematicMoved);
  }

  private void UnregisterForOtherDevicesEvents()
  {
    foreach (KeyValuePair<string, ITxDevice> keyValuePair in this.m_otherDevice)
      keyValuePair.Value.KinematicMoved -= new TxDevice_KinematicMovedEventHandler(this.device_KinematicMoved);
  }

  private void device_KinematicMoved(object sender, TxDevice_KinematicMovedEventArgs args)
  {
    this.nodeObject_KinematicMoved(sender, args);
  }

  private void RegisterForEvents()
  {
    foreach (CUiJointNodeData joint in this.Joints)
    {
      joint.SliderControl.ValueChanged += new TxWheel.TxWheel_ValueChangedEventHandler(this.SliderControl_ValueChanged);
      joint.SliderControl.DragWheelStart += new TxWheel.TxWheel_DragWheelEventHandler(this.SliderControl_DragWheelStart);
      joint.SliderControl.DragWheelEnd += new TxWheel.TxWheel_DragWheelEventHandler(this.SliderControl_DragWheelEnd);
      joint.ValueControl.ValueChanged += new RoutedEventHandler(this.ValueControl_ValueChanged);
    }
    foreach (CUiDeviceNodeData childDevice in this.ChildDevices)
      childDevice.JointChanged += new CUiDeviceNodeData.CUiRJRobotJogDeviceNodeDataJointChangedHandler(this.deviceData_JointChanged);
    this.RegisterForOtherDevicesEvents();
    this.Device.KinematicMoved += new TxDevice_KinematicMovedEventHandler(this.nodeObject_KinematicMoved);
  }

  private void UnregisterForEvents()
  {
    foreach (CUiJointNodeData joint in this.Joints)
    {
      joint.SliderControl.ValueChanged -= new TxWheel.TxWheel_ValueChangedEventHandler(this.SliderControl_ValueChanged);
      joint.SliderControl.DragWheelStart += new TxWheel.TxWheel_DragWheelEventHandler(this.SliderControl_DragWheelStart);
      joint.SliderControl.DragWheelEnd += new TxWheel.TxWheel_DragWheelEventHandler(this.SliderControl_DragWheelEnd);
      joint.ValueControl.ValueChanged -= new RoutedEventHandler(this.ValueControl_ValueChanged);
    }
    foreach (CUiDeviceNodeData childDevice in this.ChildDevices)
      childDevice.JointChanged -= new CUiDeviceNodeData.CUiRJRobotJogDeviceNodeDataJointChangedHandler(this.deviceData_JointChanged);
    this.UnregisterForOtherDevicesEvents();
    this.Device.KinematicMoved -= new TxDevice_KinematicMovedEventHandler(this.nodeObject_KinematicMoved);
  }

  public override void Dispose()
  {
    if (this.m_disposed)
      return;
    this.UnregisterForEvents();
    this.m_disposed = true;
    base.Dispose();
  }

  public delegate void CUiRJRobotJogDeviceNodeDataJointChangedHandler(object sender, EventArgs args);
}
